package com.gadarts.parashoot.enemies;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Bitmap;

import com.gadarts.parashoot.GameModel;
import com.gadarts.parashoot.GameView;
import com.gadarts.parashoot.Sprite;
import com.gadarts.parashoot.utils.C;
import com.gadarts.parashoot.utils.Utils;
import com.gadarts.parashoot.utils.ViewUtils;
import com.gadarts.parashoot.weapons.Bullet;

public abstract class AirCraftEnemy extends Enemy {

	protected boolean hasParatroopers;
	private int startSpeed;// The first speed value.
	private String crushSmokeTimer;
	private String paratrooperTimer;

	public AirCraftEnemy(GameModel model, GameView view, ArrayList<Sprite> sprites,
			int x, int y, int speed, int direction, int depth, int health,
			boolean fitFrameToDirection, Bitmap part) {
		super(model, view, sprites, x, y, speed, direction, depth, health,
				fitFrameToDirection, part);
		super.child = this;
		startSpeed = speed;
		String string = Utils.setTimer(model, C.Enemies.CRUSH_SMOKE_TIMER_NAME);
		crushSmokeTimer = Utils.setTimer(model, string);
	}

	public AirCraftEnemy(GameModel model, GameView view, Sprite sprite, int x,
			int y, int speed, int direction, int depth, int health,
			boolean fitFrameToDirection, Bitmap part) {
		super(model, view, sprite, x, y, speed, direction, depth, health,
				fitFrameToDirection, part);
		super.child = this;
		startSpeed = speed;
		String string = Utils.setTimer(model, C.Enemies.CRUSH_SMOKE_TIMER_NAME);
		crushSmokeTimer = Utils.setTimer(model, string);
	}

	@Override
	public void update() {
		super.update();
		releaseParatrooper();
		checkHealth();
		checkCollisions();
		if (gravityStatus)
			crush();
	}

	private void releaseParatrooper() {
		if (hasParatroopers && model.getSoldiers().size() <= 5) {
			if (paratrooperTimer == null)
				paratrooperTimer = Utils.setTimer(model,
						C.Enemies.Paratroopers.Infantry.RELEASE_TIMER_NAME);
			if (Utils.checkTiming(model, paratrooperTimer,
					C.Enemies.AirCrafts.Osprey.PARATROOPER_RELEASE_TIME)) {
				model.createParatrooper(getCenterX(), getCenterY());
				Utils.updateTimer(model, paratrooperTimer);
			}
		}
	}

	@Override
	protected void move() {
		super.move();
		if (direction == 0)// Face image right or left.
			matrix.preScale(-1.0f, 1.0f, getWidth() / 2, getHeight() / 2);
	}

	@Override
	protected void positionAndRotate() {
		matrix.setTranslate(x, y);
	}

	@Override
	protected void checkHealth() {
		if (health <= 0)
			gravityStatus = true;
	}

	// The enemy falls if it's crushing.
	protected void crush() {
		speed = startSpeed * 2;
		emitSmoke();
	}

	// Used for when crushing.
	private void emitSmoke() {
		int num = C.Enemies.CRUSH_SMOKE_TIMING;
		if (Utils.checkTiming(model, crushSmokeTimer, num)) {
			int randomX = x + random.nextInt(getWidth());
			int randomY = y + random.nextInt(getHeight());
			model.createSmoke(randomX, randomY, 0);
			Utils.updateTimer(model, crushSmokeTimer);
		}
	}

	// Checks collisions between objects.
	protected void checkCollisions() {
		List<Bullet> bulletsList = model.getBulletsList();
		// Check collisions between with bullets.
		for (int j = 0; j < bulletsList.size(); j++) {
			Bullet currentBullet = bulletsList.get(j);
			if (ViewUtils.checkCollision(this, currentBullet.getCenterX(),
					currentBullet.getCenterY())) {
				setHealth(health - C.Weapons.CannonBall.DAMAGE);
				model.createExplosion(currentBullet.getX(),
						currentBullet.getY(), 0);
				createRandomFlyingParts(4, false);
				model.createSmoke(currentBullet.getX(), currentBullet.getY(), 1);

				if (health <= 0) {
					createRandomFlyingParts(4, false);
					int numberOfParts = random.nextInt(4) + 2;
					// Create small explosions all over the enemy.
					for (int i = 0; i < numberOfParts; i++) {
						int randomX = random.nextInt(currentImage.getWidth());
						int randomY = random.nextInt(currentImage.getHeight());
						model.createExplosion(x + randomX, y + randomY, 0);
					}
				}

				model.destroyObject(currentBullet, Bullet.class);
				break;
			}
		}
		if (isTouchingGround())
			setHealth(-C.Enemies.AirCrafts.Scout.EXPLOSION_DAMAGE_AMOUNT);
	}
}
